package com.leruan.aasimp.action;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;

import org.apache.struts2.ServletActionContext;

import com.leruan.aasimp.entity.CarInsuranceCom;
import com.leruan.aasimp.entity.CarMainInfo;
import com.leruan.aasimp.entity.Eauser;
import com.leruan.aasimp.service.EauserService;
import com.leruan.aasimp.util.DateUtils;
import com.leruan.aasimp.util.SecurityUtils;
import com.leruan.aasimp.util.StringUtil;
import com.leruan.aasimp.util.Validators;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class EauserAction extends ActionSupport {

	private EauserService eauserService;

	public String register() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String userId = StringUtil.getUUID();
		String username = request.getParameter("username");
		String nickname = request.getParameter("nickname");
		Long userType = Long.parseLong(request.getParameter("userType"));
		String password = request.getParameter("password");
		String name = request.getParameter("name");
		String usercard = request.getParameter("usercard");
		Long tel = Long.parseLong(request.getParameter("tel"));
		String email = request.getParameter("email");
		String sex = request.getParameter("sex");
		String birthday = request.getParameter("birthday");
		String addr = request.getParameter("addr");
		String selfIntroduction = request.getParameter("selfIntroduction");
		Long isShowEmail = null;
		
		Long idCompany = null;
		String companyname = request.getParameter("companyname");
		String companyadd = request.getParameter("companyadd");
		String companyphone = request.getParameter("companyphone");
		String url = request.getParameter("url");
		String pecture = request.getParameter("pecture");
		
		try {
			isShowEmail = Long.parseLong(request.getParameter("isShowEmail"));
		} catch (NumberFormatException e) {

		}
		
		try {
			idCompany = Long.parseLong(request.getParameter("idCompany"));
		} catch (NumberFormatException e) {
			
		}
		Date registerDate = new Date();
		Eauser eauser = new Eauser(userId, username, nickname, DateUtils
				.string2Date(birthday), password, 3l, email, 0l, 0l, "", sex,
				selfIntroduction, registerDate, userType, isShowEmail, name,
				usercard, tel, addr);
		validateParameter(eauser);
		if (hasErrors()) {
			return "register";
		}
		if(idCompany!=null){
			eauser.setIdCompany(idCompany);
			CarInsuranceCom com = new CarInsuranceCom(idCompany, companyname,
					 companyphone,  pecture,  url,  companyadd,
					 userType+"",  "0");
			eauserService.saveCom(com);
		}
		eauserService.addEauser(eauser);

		return "login";
	}

	public String update() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String username = request.getParameter("username");
		String nickname = request.getParameter("nickname");
		Long userType = Long.parseLong(request.getParameter("userType"));
		String name = request.getParameter("name");
		String usercard = request.getParameter("usercard");
		Long tel = Long.parseLong(request.getParameter("tel"));
		String email = request.getParameter("email");
		String sex = request.getParameter("sex");
		String birthday = request.getParameter("birthday");
		String addr = request.getParameter("addr");
		String selfIntroduction = request.getParameter("selfIntroduction");
		Long isShowEmail = null;
		
		Long idCompany = null;
		String companyname = request.getParameter("companyname");
		String companyadd = request.getParameter("companyadd");
		String companyphone = request.getParameter("companyphone");
		String url = request.getParameter("url");
		String pecture = request.getParameter("pecture");
		
		try {
			isShowEmail = Long.parseLong(request.getParameter("isShowEmail"));
		} catch (NumberFormatException e) {
			
		}
		try {
			idCompany = Long.parseLong(request.getParameter("idCompany"));
		} catch (NumberFormatException e) {
			
		}
		Eauser eauser = eauserService.getEauserByUn(username);

		CarInsuranceCom com = new CarInsuranceCom();
		if(eauser.getIdCompany()!=null){
			com = eauserService.getComById(eauser.getIdCompany());
			if(idCompany != null){
				com.setIdCompany(idCompany);
				com.setCompanyname(companyname);
				com.setCompanyAdd(companyadd);
				com.setCompanyphone(companyphone);
				com.setPecture(pecture);
				com.setType(userType+"");
				com.setUrl(url);
				eauserService.updateCom(com);
			}
		}else{
			if(idCompany != null){
				com.setIdCompany(idCompany);
				com.setCompanyname(companyname);
				com.setCompanyAdd(companyadd);
				com.setCompanyphone(companyphone);
				com.setPecture(pecture);
				com.setUrl(url);
				com.setType(userType+"");
				com.setStatus("0");
				eauserService.saveCom(com);
			}
		}
		eauser.setIdCompany(idCompany);
		eauser.setNickname(nickname);
		eauser.setUserType(userType);
		eauser.setName(name);
		eauser.setUsercard(usercard);
		eauser.setTel(tel);
		eauser.setEmail(email);
		eauser.setSex(sex);
		eauser.setBirthday(DateUtils.string2Date(birthday));
		eauser.setAddress(addr);
		eauser.setSelfIntroduction(selfIntroduction);
		eauser.setIsshowemail(isShowEmail);
		
		validateParameter(eauser);
		if (hasErrors()) {
			request.setAttribute("message","修改失败！");
		}else{
			eauserService.updateEauser(eauser);
			request.setAttribute("message","修改成功！");
			request.getSession().setAttribute("eauser", eauser);
			request.getSession().setAttribute("companyInfo", com);
		}
		return "update";
	}

	private void validateParameter(Eauser eauser) {
		if (Validators.isEmpty(eauser.getUsername())) {
			addActionError("用户名不能为空");
		}
		if (Validators.isEmpty(eauser.getNickname())) {
			addActionError("昵称不能为空");
		}
		if (Validators.isEmpty(eauser.getPassword())) {
			addActionError("密码不能为空");
		}
		if (Validators.isEmpty(eauser.getName())) {
			addActionError("姓名不能为空");
		}
		if (Validators.isEmpty(eauser.getUsercard())) {
			addActionError("身份证号不能为空");
		}
		if (Validators.isEmpty(eauser.getEmail())) {
			addActionError("Email不能为空");
		}
	}

	public String updateInfo() throws UnsupportedEncodingException {
		ActionContext context = ServletActionContext.getContext();
		HttpServletRequest request = (HttpServletRequest) context
				.get(ServletActionContext.HTTP_REQUEST);
		String username = request.getParameter("username");
		String name = request.getParameter("name").trim();
		System.out.println(name);
		// name = new String(name.getBytes("iso-8859-1"),"utf-8");
		String email = request.getParameter("email");
		Long tel = Long.parseLong(request.getParameter("tel"));

		Eauser eauser = eauserService.getEauserByUn(username);
		eauser.setName(name);
		eauser.setEmail(email);
		eauser.setTel(tel);

		eauserService.updateEauser(eauser);
		request.getSession().setAttribute("eauser", eauser);
		request.setAttribute("message", "修改成功！");
		return "personInfo";
	}

	public String updatePwd() throws UnsupportedEncodingException {
		ActionContext context = ServletActionContext.getContext();
		HttpServletRequest request = (HttpServletRequest) context
				.get(ServletActionContext.HTTP_REQUEST);
		Eauser eauser = (Eauser) request.getSession().getAttribute("eauser");
		String pwd0 = request.getParameter("pwd0");
		String pwd1 = request.getParameter("pwd1");

		System.out.println(pwd0);
		System.out.println(pwd1);
		pwd0 = SecurityUtils.encodeByMD5(pwd0);
		pwd1 = SecurityUtils.encodeByMD5(pwd1);
		System.out.println(pwd0);
		System.out.println(pwd1);
		if (pwd0.equals(eauser.getPassword())) {
			eauser.setPassword(pwd1);
			eauserService.updateEauser(eauser);
			request.getSession().setAttribute("eauser", eauser);
			request.setAttribute("message", "密码修改成功！");
		} else {
			request.setAttribute("message", "旧密码输入有误！");
		}

		return "pwd";
	}

	public String login() {
		ActionContext context = ServletActionContext.getContext();
		HttpServletRequest request = (HttpServletRequest) context
				.get(ServletActionContext.HTTP_REQUEST);
		String username = request.getParameter("username").trim();
		String password = request.getParameter("password");
		String flag = request.getParameter("flag");

		Eauser eauser = eauserService.getEauserByUn(username);
		if (eauser == null) {
			request.setAttribute("message", "用户名不存在！");
			if(flag==null){
				return "login";
			}else{
				return "backlogin";
			}
		}
		String pwd = SecurityUtils.encodeByMD5(password);
			
		if (eauser.getPassword().equals(pwd)) {
			if(eauser.getIdCompany()!=null){
				CarInsuranceCom com = eauserService.getComById(eauser.getIdCompany());
				System.out.println(com.getCompanyname());
				request.getSession().setAttribute("companyInfo", com);
			}
			if(flag==null){
				request.getSession().setAttribute("eauser", eauser);
				return "login";
			}else if(eauser.getUserType()==2){
				request.setAttribute("message", "对不起！您没有权限登录后台。");
				return "backlogin";
			}else{
				request.getSession().setAttribute("eauser", eauser);
				return "backindex";
			}
		} else {
			request.setAttribute("message", "密码输入有误！");
			if(flag==null){
				return "login";
			}else{
				return "backlogin";
			}
		}
	}

	public String logout() {
		ActionContext context = ServletActionContext.getContext();
		HttpServletRequest request = (HttpServletRequest) context
				.get(ServletActionContext.HTTP_REQUEST);
		request.getSession().removeAttribute("eauser");
		request.getSession().removeAttribute("companyInfo");
		request.getSession().removeAttribute("carMainInfo");
		String flag = request.getParameter("flag");
		if(flag==null){
			return "login";
		}else{
			return "backlogin";
		}
	}
	
	public String baseInfo() {
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("flag", "1");
		return "register";
	}
	
	public String carManage() {
		HttpServletRequest request = ServletActionContext.getRequest();
		Eauser eauser = (Eauser) request.getSession().getAttribute("eauser");
		String usercard = eauser.getUsercard();
		List<CarMainInfo> carMain = eauserService.getCarManagesByUc(usercard);
		JSONArray array = JSONArray.fromObject(carMain);
		System.out.println(array.toString()); 
		request.setAttribute("carMain", array.toString().replace("\"", "'"));
		request.setAttribute("flag", "1");
		return "carManage";
	}
	
	public String updatecarManage() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String vin = request.getParameter("id");
		String plateNum = request.getParameter("plateNum");
		
		CarMainInfo carMainInfo = eauserService.getCarManagesByVin(vin);
		carMainInfo.setPlateNum(plateNum);
		eauserService.updatecarManage(carMainInfo);
		return carManage();
	}


	public EauserService getEauserService() {
		return eauserService;
	}

	public void setEauserService(EauserService eauserService) {
		this.eauserService = eauserService;
	}


}
